#include "writeVtk.h"
#include <iostream>
#include <fstream>

void SG::FEMSolver::Element::writeVtk (const Meshes& meshes, const std::string& filename, const std::string& discription)
{
    auto nodeSize = meshes.nodes.size ();
    auto eleSize = meshes.q9Elements.size ();

    std::ofstream out;
    out.open (filename + ".vtk");

    // Write header, title, data type
    out << "# vtk DataFile Version 3.0\n";
    out << discription << "\n";
    out << "ASCII\n";

    // Write DATASET
    out << "\nDATASET UNSTRUCTURED_GRID\n";

    // Write POINTS
    out << "POINTS "<< nodeSize	<< " float\n";
    for (auto& node : meshes.nodes)
    {
        out << 0.0 << " " << node.y << " " << node.z << "\n";
    }

    // Write CELLS
    out << "CELLS " << eleSize << " " << eleSize * 9 << "\n";
    for (auto& ele : meshes.q9Elements)
    {
        out << "8" << " "
            << ele.nodes[0] - 1 << " "
            << ele.nodes[1] - 1 << " "
            << ele.nodes[2] - 1 << " "
            << ele.nodes[3] - 1 << " "
            << ele.nodes[4] - 1 << " "
            << ele.nodes[5] - 1 << " "
            << ele.nodes[6] - 1 << " "
            << ele.nodes[7] - 1 << " "
            << std::endl;
    }

    // Write CELL_TYPES
    out << "CELL_TYPES " << eleSize << "\n";
    for (std::size_t i= 0; i < eleSize; ++i)
    {
        out << 23 << "\n";
    }

    out.close ();
}